package edu.gatech.oad.mule;

import java.util.Random;

import edu.gatech.oad.mule.model.GameStateMachine.GameState;
import edu.gatech.oad.mule.view.interfaces.RenderableBuilding;

/**
 * This class represents the pub/bar inside of the town.  When a Player goes 
 * into the bar, they are guaranteed to win money. The amount of money is 
 * determined based upon how much time the Player has left in his/her turn.
 *
 * @author Brendan McGarry, Dale Avery, D'Miria Collins, Natasha Parekh, Jaemin Park
 * @version %I%, %G%
 */
public class Bar extends Zone implements RenderableBuilding{
	
	//random object for calculating time bonuses
	private Random r;

	//monetary reward that depends on what round it is
	private int[] roundBonus = {50, 50, 50, 100, 100, 100, 100, 150, 150, 150, 150, 200};

	//the most you can win at the pub
	public static final int MAX_REWARD = 250;

	//the amount the player has won by entering the bar
	private int reward;

	/**
	 * This constructor creates a new bar building in
	 * the town.  It just passes everything up to 
	 * Zone
	 *
	 * @param x The x coordinate of the bar
	 * @param y The y coordinate of the bar
	 * @param width The width of the bar
	 * @param height The height of the bar
	 */
	public Bar(int x, int y, int width, int height) {
		super(x, y, width, height);
		r = new Random();
		reward = 0;
	}//end constructor
	
	/**
	 * Calculates the amount of money the player won gambling in the bar
	 */
	private void calcReward(int currentRound, int timeLeft){
	
		//calculate the amount of money the player wins.
		//  this follows the following formula:
		//
		//  Money Bonus = Round Bonus + Random(0, Time Bonus)
		//
		//The time bonus is a reward given based on how much time
		//  is left in the players turn

		//safety things
		if(currentRound < 0){
			currentRound = 0;
		}//end if
		else if(currentRound >= roundBonus.length){
			currentRound = roundBonus.length - 1;
		}//end else if
		
		//more than 37s left in turn
		if(timeLeft >= 37){
				reward = roundBonus[currentRound] + r.nextInt(200);
		}//end if
		//[25, 37) seconds left
		else if(timeLeft >= 25){
			reward = roundBonus[currentRound] + r.nextInt(150);
		}//end else if
		//[12, 25) seconds left
		else if(timeLeft >= 12){
			reward = roundBonus[currentRound] + r.nextInt(100);
		}//end else if
		//[0, 12) seconds left
		else{
			reward = roundBonus[currentRound] + r.nextInt(50);
		}//end else

		//there are limits on how much a player can win
		if(reward > MAX_REWARD){
			reward = MAX_REWARD;
		}//end if
		
	}//end method calcReward
	
	@Override
	public void interact(Player p, GameState phase, int round, int time){
		System.out.println("Time: " + time + "ms");
		System.out.println("Round: " + round);
		System.out.print(p.getName() + " enters the bar with [" + p.getMoney() + "]. ");
		calcReward(round, (time/1000));
		p.addMoney(reward);
		System.out.print(p.getName() + " won [" + reward + "] gambling. ");
		System.out.println("New balance: [" + p.getMoney() + "]");
	}//end method interact
	
	//====================
	//	RenderableBuilding
	//====================
		
	@Override
	public BuildingType getBuildingType(){
		return BuildingType.BAR;
	}//end method getBuildingType
		
	@Override
	public int getCost(){
		return reward;
	}//end method getCost

}//end class Bar



//====================================
//	do we still need this code?
//====================================
/*
 * int[] roundBonusList = {50, 50, 50, 100, 100, 100, 100, 150, 150, 150, 150, 200}; 
 */
/**
 * This constructor configures a new Bar object
 */
/*
	public Bar(int x, int y, int width, int height){
		super(x, y, width, height);//do stuff
	}//end constructor

	public Bar(Rectangle r) {
		super(r);
	}

	public void enterBuilding() {
		System.out.println("You have entered the bar");
	}

	//figure out how to reference gamedriver
	public int calculateGambleOutcome() {
		int timeBonus;
		int roundBonus;
		
		if(GameDriver.getPlayerTime() >= 37) {
			timeBonus = 200;
		}
		else if(GameDriver.getPlayerTime() >= 25) {
			timeBonus = 150;
		}
		else if(GameDriver.getPlayerTime() >= 12) {
			timeBonus = 100;
		}
		else
			timeBonus = 50;
	
		roundBonus = roundBonusList[GameDriver.getCurrentRound() - 1];
		
		int moneyBonus = roundBonus + (int)(Math.random() * timeBonus);
		
		return moneyBonus;
	}//end method calculateGambleOutcome
*/
